/*
 * SessionComboBoxModel.java
 * 
 * Created on Aug 17, 2007, 2:09:49 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oslo.ui.util;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;
import org.oslo.ui.util.SessionComboBoxModel.SessionLocationPair;

/**
 * Simpel combo box model that lists a number of conferences 
 * and there locations.
 * 
 * @author nl24167
 */
public class SessionComboBoxModel implements ComboBoxModel {

    private List<SessionLocationPair> valuesList = new ArrayList<SessionLocationPair>();
    private SessionLocationPair selectedItem = null;
    private List<ListDataListener> listeners = new ArrayList<ListDataListener>();
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    
    public SessionComboBoxModel() {
        valuesList.add(new SessionLocationPair("JavaOne", "Howard street 747,San Francisco,CA,US"));
        valuesList.add(new SessionLocationPair("JavaPolis", "Groenendaallaan 394,Antwerp,Belgium"));
        valuesList.add(new SessionLocationPair("JavaZone", "Sonja Henies plass 2,Oslo,Norway"));
        selectedItem = valuesList.get(0);
    }

    public void setSelectedItem(Object anItem) {
        SessionLocationPair oldItem = selectedItem;
        selectedItem = (SessionLocationPair) anItem;
        
        changeSupport.firePropertyChange("selectedItem",oldItem, selectedItem);
        changeSupport.hasListeners("selectedItem");
        
    }

    public Object getSelectedItem() {
        return selectedItem;
    }

    public int getSize() {
        return valuesList.size();
    }

    public Object getElementAt(int index) {
        return valuesList.get(index);
    }
    
    public void addListDataListener(ListDataListener l) {
        listeners.add(l);
    }

    public void removeListDataListener(ListDataListener l) {
        listeners.remove(l);
    }
        
    public void addPropertyChangeListener( PropertyChangeListener l ) {
        changeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener( PropertyChangeListener l ) {
        changeSupport.removePropertyChangeListener(l);
    }    

    /**
     * Class containing the session location pair
     */ 
    public static class SessionLocationPair {
        
        private String name;
        private String location;

        public SessionLocationPair(String name, String location) {
            this.name = name;
            this.location = location;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return getName();
        }
    }
}
